package com.scrm.server.wx.cp.handler.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scrm.api.wx.cp.dto.WxCpXmlOutMessageDTO;
import com.scrm.api.wx.cp.dto.WxCustomerTagSaveOrUpdateDTO;
import com.scrm.api.wx.cp.entity.WxCustomer;
import com.scrm.api.wx.cp.entity.WxFissionContact;
import com.scrm.api.wx.cp.entity.WxFissionTask;
import com.scrm.common.constant.Constants;
import com.scrm.common.util.ListUtils;
import com.scrm.server.wx.cp.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：qiuzl
 * @date ：Created in 2022/4/2 14:36
 * @description：应用宝欢迎语
 **/
@Slf4j
@Service
public class FissionUpdateCustomerHandler extends CommonUpdateCustomerHandler {

    @Autowired
    private IWxFissionTaskService wxFissionTaskService;

    @Autowired
    private IWxFissionCustomerService wxFissionCustomerService;

    @Autowired
    private IWxFissionContactService wxFissionContactService;

    @Autowired
    private IWxFissionTaskCustomerDetailService fissionTaskCustomerDetailService;

    @Autowired
    private IWxCustomerService customerService;

    @Autowired
    private IStaffService staffService;

    @Override
    public int getSort() {
        return 0;
    }

    @Override
    public boolean match(String extCorpId, String extStaffId, String state) {
        return getFissionContact(extCorpId, state) != null;
    }

    @Override
    @Transactional
    public void saveHandle(WxCpXmlOutMessageDTO dto) {

        //不用判空，match已判了
        WxFissionContact contact = getFissionContact(dto.getExtCorpId(), dto.getState());

        WxFissionTask task = wxFissionTaskService.checkExists(contact.getTaskId());

        sendMsg(dto, task.getMsg());

        //新增一条
        wxFissionCustomerService.refreshCustomer(dto.getExtCorpId(), dto.getExternalUserId(), dto.getUserId(), dto.getIsNewCustomer(), contact);

        //打标签
        if (ListUtils.isNotEmpty(task.getTags())) {
            WxCustomer wxCustomer = customerService.checkExists(dto.getExtCorpId(), dto.getExternalUserId());

            WxCustomerTagSaveOrUpdateDTO updateDTO = new WxCustomerTagSaveOrUpdateDTO()
                    .setId(wxCustomer.getId()).setExtCorpId(wxCustomer.getExtCorpId())
                    .setAddTags(task.getTags()).setOrigin(Constants.DYNAMIC_TAG_TYPE_JOIN_TASK)
                    .setOperatorExtId(dto.getUserId())
                    .setStaffId(staffService.checkExists(dto.getUserId(), dto.getExtCorpId())
                            .getId());

            customerService.queueEditTag(updateDTO);

        }


    }

    @Override
    public void deleteHandle(WxCpXmlOutMessageDTO dto) {
        //企微应用宝判断
        wxFissionCustomerService.updateCustomerLose(dto.getExtCorpId(), dto.getExternalUserId(), dto.getUserId());
        fissionTaskCustomerDetailService.customerUpdate(dto.getExtCorpId(), dto.getExternalUserId());
    }

    private WxFissionContact getFissionContact(String extCorpId, String state){

        //先查渠道码
        WxFissionContact contact = wxFissionContactService.getOne(new QueryWrapper<WxFissionContact>().lambda()
                .eq(WxFissionContact::getExtCorpId, extCorpId)
                .eq(WxFissionContact::getState, state), false);

        if (contact == null || contact.getId() == null) {
            return null;
        }

        //再查有没有过期
        WxFissionTask wxFissionTask = wxFissionTaskService.checkExists(contact.getTaskId());

        if (wxFissionTask.getStatus().equals(WxFissionTask.STATUS_OVER)) {
            return null;
        }else{
            return contact;
        }
    }
}
